package com.stox.navigator.ui.swing.widget;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BoxLayout;
import javax.swing.tree.DefaultMutableTreeNode;

import com.stox.core.ui.swing.wrapper.JButton;
import com.stox.core.ui.swing.wrapper.JPanel;
import com.stox.core.ui.swing.wrapper.JTextField;
import com.stox.core.ui.swing.wrapper.JTree;
import com.stox.design.Holder;
import com.stox.navigator.ui.swing.event.JTreeSearchTextFieldKeyAdapter;
import com.stox.resources.i18n.MessageSource;

public class JTreeSearchPanel extends JPanel {
	private static final long serialVersionUID = -8752228125432380416L;

	private JTextField searchTextField = new JTextField(MessageSource.getMessage("Search"));
	private JButton nextButton = new JButton(null, MessageSource.getMessage("Next"), "go_right.png", KeyEvent.VK_N);
	private JButton previousButton  = new JButton(null, MessageSource.getMessage("Previous"), "go_left.png", KeyEvent.VK_P);
	private JButton lastButton = new JButton(null, MessageSource.getMessage("Last"), "go_last.png", KeyEvent.VK_L);
	private JButton firstButton = new JButton(null, MessageSource.getMessage("First"), "go_first.png", KeyEvent.VK_F);
	private boolean busy = false;
	private Holder<JTree> holder;
	
	public JTreeSearchPanel(final Holder<JTree> holder, KeyListener delegateKeyListener) {
		this.holder = holder;
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		Dimension dimension = new Dimension(30,30);
		searchTextField.setHorizontalAlignment(JTextField.CENTER);
		add(searchTextField);
		nextButton.setPreferredSize(dimension);
		nextButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				next();
			}
		});
		add(nextButton);
		previousButton.setPreferredSize(dimension);
		previousButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				previous();
			}
		});
		add(previousButton);
		lastButton.setPreferredSize(dimension);
		lastButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				last();
			}
		});
		add(lastButton);
		firstButton.setPreferredSize(dimension);
		firstButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				first();
			}
		});
		add(firstButton);
		
		searchTextField.addKeyListener(new JTreeSearchTextFieldKeyAdapter(holder, delegateKeyListener));
	}

	public void next(){
		if(!busy){
			try{
				busy = true;
				JTree tree = holder.get();
				DefaultMutableTreeNode node = null;
				if(tree.getSelectionPath() == null){
					node = (DefaultMutableTreeNode)tree.getModel().getRoot();
				}else{
					node = (DefaultMutableTreeNode)tree.getSelectionPath().getLastPathComponent();
				}
				node = node.getNextNode();
				while(node != null && 
						!node.toString().toUpperCase().contains(
								searchTextField.getText().toUpperCase())){
					node = node.getNextNode();
				}
				if(node != null){
					tree.selectAndShow(node);
				}
			}finally{
				busy = false;
			}
		}
	}
	
	
	public void previous(){
		if(!busy){
			try{
				busy = true;
				JTree tree = holder.get();
				DefaultMutableTreeNode node = null;
				if(tree.getSelectionPath() == null){
					node = (DefaultMutableTreeNode)tree.getModel().getRoot();
					node = node.getLastLeaf();
				}else{
					node = (DefaultMutableTreeNode)tree.getSelectionPath().getLastPathComponent();
				}
				node = node.getPreviousNode();
				while(node != null && 
						!node.toString().toUpperCase().contains(
								searchTextField.getText().toUpperCase())){
					node = node.getPreviousNode();
				}
				if(node != null){
					tree.selectAndShow(node);
				}
			}finally{
				busy = false;
			}
		}
	}
	
	public void first(){
		if(!busy){
			try{
				busy = true;
				JTree tree = holder.get();
				DefaultMutableTreeNode node = null;
				node = (DefaultMutableTreeNode)tree.getModel().getRoot();
				node = node.getNextNode();
				while(node != null && 
						!node.toString().toUpperCase().contains(
								searchTextField.getText().toUpperCase())){
					node = node.getNextNode();
				}
				if(node != null){
					tree.selectAndShow(node);
				}
			}finally{
				busy = false;
			}
		}
	}
	
	public void last(){
		if(!busy){
			try{
				busy = true;
				JTree tree = holder.get();
				DefaultMutableTreeNode node = null;
				node = (DefaultMutableTreeNode)tree.getModel().getRoot();
				node = node.getLastLeaf();
				while(node != null && 
						!node.toString().toUpperCase().contains(
								searchTextField.getText().toUpperCase())){
					node = node.getPreviousNode();
				}
				if(node != null){
					tree.selectAndShow(node);
				}
			}finally{
				busy = false;
			}
		}
	}
	
	public JTextField getSearchTextField() {
		return searchTextField;
	}
}
